package com.imoocdl.Handler;

import com.imoocdl.VO.ResultVO;
import com.imoocdl.config.ProjectUrl;
import com.imoocdl.exception.ResponsBankException;
import com.imoocdl.exception.SellException;
import com.imoocdl.exception.SellerAuthorizeException;
import com.imoocdl.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局异常处理拦截
 * Created by pc on 2018/7/27.
 */
@ControllerAdvice
public class SellerExceptionHandler {
    @Autowired
    private ProjectUrl projectUrl;
    //拦截登录异常
    @ExceptionHandler(value = SellerAuthorizeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ModelAndView handlerAuthorizeException(){
        return new ModelAndView("redirect:"
            .concat(projectUrl.getWechatOpenAuthorize())
            .concat("/sell/wechat/qrAuthorize")
            .concat("?returnUrl=")
            .concat(projectUrl.getSell())
            .concat("/sell/seller/login"));
    }

    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVO handlerSellerException(SellException e){
        return ResultVOUtil.error(e.getCode(),e.getMessage());
    }

    /**
     * 将返回的 status状态改为403 message=测试错误
     */
    @ExceptionHandler(value = ResponsBankException.class)
    @ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "测试错误")
    public void handlerResponsBankException(){

    }
}
